//
//  MainTBC.m
//  EPTool
//
//  Created by guoshiqing on 2018/4/3.
//  Copyright © 2018年 云数动能. All rights reserved.
//

#import "MainTBC.h"
#import "AppDelegate.h"
#import "Macro.h"

#import "BaseNavigationController.h"

#import "BaseVC.h"
#import "BaseViewModel.h"
#import "ToastView.h"
#import "CommonHelper.h"
#import "NotificationHelp.h"
#import "SocketManager.h"
#import "BaseLongLinkService.h"
#import "ErrorCode.h"

// Item
#import "PersonalCenterVC.h"


@interface MainTBC () <UINavigationControllerDelegate>

// 视图模型
@property (nonatomic, strong) BaseViewModel *accountVM;

// 页面1 (如果需要隐藏左上角的返回按钮，BaseVC的子类添加[self hideBackButton])
@property (nonatomic, strong) BaseVC *page1VC;
@property (nonatomic, strong) BaseNavigationController *navPage1;
// 页面2
@property (nonatomic, strong) BaseVC *page2VC;
@property (nonatomic, strong) BaseNavigationController *navPage2;
// 页面3
@property (nonatomic, strong) BaseVC *page3VC;
@property (nonatomic, strong) BaseNavigationController *navPage3;
// 页面4
@property (nonatomic, strong) PersonalCenterVC *meVC;
@property (nonatomic, strong) BaseNavigationController *meNavPage;

@end

@implementation MainTBC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 添加接口模型
    [self createViewModel];
    
    if (![CommonHelper shared].isPassiveAwake) {
        [self autoLogin];
    }
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.viewControllers = @[self.navPage1, self.navPage2, self.navPage3, self.meNavPage];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - 处理网络请求
- (void)createViewModel
{
    __weak typeof(self) weakSelf = self;
    [self.accountVM setBlockWithReturnBlock:^(BaseJSONEntity *returnValue) {
        // 登录成功
    } WithErrorBlock:^(BaseJSONEntity *errorValue) {
        if (C_BeKick == errorValue.ret) {
            [TheAppDel go2LoginVC:nil];
        }
        else
        {
            // 弱网下超时，只能提示错误，不能退登
            [ToastView showMsg:errorValue.msg inView:weakSelf.view];
        }
    } WithFailureBlock:^(BaseJSONEntity *failureValue){
        // 网络不可用时，只能提示错误，不能退登
        [ToastView showMsg:failureValue.msg inView:weakSelf.view];
    }];
}

#pragma mark - 懒加载
- (BaseViewModel *)accountVM
{
    if (!_accountVM) {
        _accountVM = [[BaseViewModel alloc] init];
    }
    return _accountVM;
}

- (BaseVC *)page1VC
{
    if (!_page1VC) {
        _page1VC = [[BaseVC alloc] init];
    }
    return _page1VC;
}

- (BaseNavigationController *)navPage1
{
    if (!_navPage1) {
        _navPage1 = [self madeNavWithRootVC:self.page1VC andTitle:@"页面1" andNormalImageName:@"tabbar_icon_office_gray" andSelectImageName:@"tabbar_icon_office_blue" andTag:0];
        _navPage1.navigationBar.translucent = NO;
        [_navPage1.navigationController.navigationBar setShadowImage:[UIImage new]];
    }
    return _navPage1;
}

- (BaseVC *)page2VC
{
    if (!_page2VC) {
        _page2VC = [[BaseVC alloc] init];
    }
    return _page2VC;
}

- (BaseNavigationController *)navPage2
{
    if (!_navPage2) {
        _navPage2 = [self madeNavWithRootVC:self.page2VC andTitle:@"页面2" andNormalImageName:@"tabbar_icon_salestools_gray" andSelectImageName:@"tabbar_icon_salestools_blue" andTag:1];
        _navPage2.navigationBar.translucent = NO;
        [_navPage2.navigationController.navigationBar setShadowImage:[UIImage new]];
    }
    return _navPage2;
}

- (BaseVC *)page3VC
{
    if (!_page3VC) {
        _page3VC = [[BaseVC alloc] init];
    }
    return _page3VC;
}

- (BaseNavigationController *)navPage3
{
    if (!_navPage3) {
        _navPage3 = [self madeNavWithRootVC:self.page3VC andTitle:@"页面3" andNormalImageName:@"tabbar_icon_customer_gray" andSelectImageName:@"tabbar_icon_customer_blue" andTag:2];
        _navPage3.navigationBar.translucent = NO;
        [_navPage3.navigationController.navigationBar setShadowImage:[UIImage new]];
    }
    return _navPage3;
}

- (PersonalCenterVC *)meVC
{
    if (!_meVC) {
        _meVC = [[PersonalCenterVC alloc] init];
    }
    return _meVC;
}

- (BaseNavigationController *)meNavPage
{
    if (!_meNavPage) {
        _meNavPage = [self madeNavWithRootVC:self.meVC andTitle:@"我" andNormalImageName:@"tabbar_icon_me_gray" andSelectImageName:@"tabbar_icon_me_blue" andTag:3];
        _meNavPage.navigationBar.translucent = NO;
        [_meNavPage.navigationController.navigationBar setShadowImage:[UIImage new]];
    }
    return _meNavPage;
}

#pragma mark - 创建一个UINavigationController
- (BaseNavigationController *)madeNavWithRootVC:(UIViewController *)rootViewController andTitle:(NSString *)title andNormalImageName:(NSString *)normal andSelectImageName:(NSString *)select andTag:(NSUInteger)tag
{
    BaseNavigationController *nav = [[BaseNavigationController alloc] initWithRootViewController:rootViewController];

    UIImage *imageNormal = [[UIImage imageNamed:normal] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIImage *imageSelect = [[UIImage imageNamed:select] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    nav.tabBarItem = [[UITabBarItem alloc] initWithTitle:title image:imageNormal selectedImage:imageSelect];
    // 改变文字颜色为蓝色
    NSDictionary *dic = @{NSForegroundColorAttributeName:[EPColor epColor_Blue], NSFontAttributeName:[UIFont systemFontOfSize:10.f]};
    [nav.tabBarItem setTitleTextAttributes:dic forState:UIControlStateSelected];
    NSDictionary *dicNormal = @{NSForegroundColorAttributeName:[EPColor epColor_descMessageGray], NSFontAttributeName:[UIFont systemFontOfSize:10.f]};
    [nav.tabBarItem setTitleTextAttributes:dicNormal forState:UIControlStateNormal];
    nav.tabBarItem.tag = tag;

    nav.tabBarItem.titlePositionAdjustment = UIOffsetMake(0, -2);
    return nav;
}

- (void)dealloc
{
    NSLog(@"%@ dealloc..",NSStringFromClass([self class]));
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

+ (instancetype)instance{
    AppDelegate *delegete = (AppDelegate *)[UIApplication sharedApplication].delegate;
    UIViewController *vc = delegete.window.rootViewController;
    if ([vc isKindOfClass:[MainTBC class]]) {
        return (MainTBC *)vc;
    }else{
        return nil;
    }
}

- (instancetype)initWithIndex:(NSUInteger)index{
    if (self = [super init]) {
        _nowIndex = index;
        self.selectedIndex = _nowIndex;
    }
    return self;
}

#pragma mark - 自动登录
- (void)autoLogin
{
    // 如果已经是登录状态，这里就不需要重新登录了
    if (TheAppDel.wasAutoLogin) {
//        [self.accountVM autoLoginRequest];
        return;
    }
}

@end
